package com.demo.openpdfdemo.pdfCreator;

import com.demo.openpdfdemo.model.UserModel;
import com.demo.openpdfdemo.pdfCreator.core.AbstractPdfCreator;
import com.demo.openpdfdemo.pdfCreator.core.HtmlParserUtil;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 用户信息导出为pdf
 */
public class UserInfoPdfCreator extends AbstractPdfCreator {

    @Override
    public void execute(Map<String, Object> map) throws DocumentException, IOException {
        //map中可以存放实体类信息，以及其他各种逻辑数据信息等
        UserModel user = (UserModel) map.get("data");
        Font titleFont = new Font(baseFont, 14, Font.BOLD);
        Paragraph title = new Paragraph("用户信息表", titleFont);
        title.setAlignment(Element.ALIGN_CENTER);
        title.setSpacingAfter(10F);
        document.add(title);
        Paragraph paragraphText = new Paragraph();
        Font font1 = new Font(baseFont, fontSize, Font.NORMAL);
        paragraphText.add(new Chunk("此表为用户基本信息，请查看", font1));
        paragraphText.setAlignment(Element.ALIGN_LEFT);
        paragraphText.setFirstLineIndent(fontSize * 2);
        document.add(paragraphText);

        PdfPTable table = new PdfPTable(4);
        table.setTotalWidth(530);
        table.setWidths(new int[]{20, 30, 22, 28});
        table.setSpacingBefore(20);
        table.setWidthPercentage(100);
        table.setLockedWidth(true);

        Paragraph p1 = new Paragraph("姓名", font);
        PdfPCell cell1 = new PdfPCell(p1);
        cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell1);
        Paragraph p2 = new Paragraph(user.getName(), font);
        PdfPCell cell2 = new PdfPCell(p2);
        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell2);

        Paragraph p3 = new Paragraph("年龄", font);
        PdfPCell cell3 = new PdfPCell(p3);
        cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell3);
        Paragraph p4 = new Paragraph(String.valueOf(user.getAge()), font);
        PdfPCell cell4 = new PdfPCell(p4);
        cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell4);

        Paragraph p5 = new Paragraph("性别", font);
        PdfPCell cell5 = new PdfPCell(p5);
        cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell5);
        Paragraph p6 = new Paragraph(user.getSex(), font);
        PdfPCell cell6 = new PdfPCell(p6);
        cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell6);

        Paragraph p7 = new Paragraph("籍贯", font);
        PdfPCell cell7 = new PdfPCell(p7);
        cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell7);
        Paragraph p8 = new Paragraph(user.getSex(), font);
        PdfPCell cell8 = new PdfPCell(p8);
        cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell8);

        Paragraph p9 = new Paragraph("民族", font);
        PdfPCell cell9 = new PdfPCell(p9);
        cell9.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell9);
        Paragraph p10 = new Paragraph(user.getSex(), font);
        PdfPCell cell10 = new PdfPCell(p10);
        cell10.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell10);

        Paragraph p11 = new Paragraph("学历", font);
        PdfPCell cell11 = new PdfPCell(p11);
        cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell11);
        Paragraph p12 = new Paragraph(user.getSex(), font);
        PdfPCell cell12 = new PdfPCell(p12);
        cell12.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell12);

        Paragraph p13 = new Paragraph("备注", font);
        PdfPCell cell13 = new PdfPCell(p13);
        cell13.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell13);

        PdfPCell cell14 = new PdfPCell();
        List<Element> elements = HtmlParserUtil.htmlParseToElementList(user.getRemark(), font);
        if (!CollectionUtils.isEmpty(elements)) {
            for (Element element : elements) {
                cell14.addElement(element);
            }
        }
        cell14.setColspan(3);
        table.addCell(cell14);

        table.setSplitLate(false);
        document.add(table);


    }


}
